<?php

namespace app\common\model\printmanager;

use think\Model;
use app\common\model\designermanager\Designer;
use app\common\model\printmanager\Org;
use app\common\model\designermanager\Production as DesignProduction;
use app\common\model\printmanager\Production;

/**
 * 印刷需求
 */
class Printing extends Model
{

    

    

    // 表名
    protected $name = 'self_printing';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'user_info',
        'category',
        'used_time_text',
        'status_text',
        'display_text'
    ];
    
    public function getUserInfoAttr($value, $data){
        $value = isset($data['user_id']) ? $data['user_id'] : 0;
        return $value ? \app\common\model\User::getBaseInfoNosense($value) : '';
    }
    
    public function getStatusList()
    {
        return ['0' => "待验证", '1' => "待下单", '2' => "无效需求", '3' => "已下单付款", '4' => "已完成"];
    }

    public function getDisplayList()
    {
        return ['0' => __('Display 0'), '1' => __('Display 1')];
    }


    public function getUsedTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['used_time']) ? $data['used_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getDisplayTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['display']) ? $data['display'] : '');
        $list = $this->getDisplayList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setUsedTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function getOrgInfoAttr($value, $data){
        $org_id = isset($data['org_id']) ? $data['org_id'] : 0;

        return $org_id ? Org::cache(300)->find($org_id) : null;
    }

    public function getDesignerInfoAttr($value, $data){
        $org_id = isset($data['designer_id']) ? $data['designer_id'] : 0;

        return $org_id ? Designer::cache(300)->find($org_id) : null;
    }

    public function getCategoryAttr($value, $data){
        $value = isset($data['category_id']) ? $data['category_id'] : 0;
        return $value ? \app\common\model\printmanager\Category::find($value) : '';
    }

    public function getFileListAttr($value, $data) {
        $value = isset($data['id']) ? $data['id'] : 0;
        $list = $value ? \app\common\model\printmanager\PrintFile::where('printing_id', $value)->select() : [];
        $res = [];
        foreach ($list as $key => $value) {
            if($value['file_url']){
                $res[] = $value;
            }
        }
        unset($key, $value, $list);
        return $res;
    }

    public function getProductInfoAttr($value, $data){
        $id = isset($data['id']) ? $data['id'] : 0;
        $org_id = isset($data['org_id']) ? $data['org_id'] : 0;
        return Production::where('pringting_id', $id)->where('org_id', $org_id)->find();
    }

    public function getDesignProductInfoAttr($value, $data){
        $id = isset($data['id']) ? $data['id'] : 0;
        $org_id = isset($data['designer_id']) ? $data['designer_id'] : 0;
        return DesignProduction::where('pringting_id', $id)->where('designer_id', $org_id)->find();
    }
}
